package org.me.marble;

import android.graphics.*;

import android.graphics.Paint.Style;
import android.graphics.Bitmap.Config;
import javax.microedition.khronos.opengles.GL10;

/**
*
* @author tungi
*/

public class BitmapText{//extends BitmapDrawable{
    private int _w,_h;
    private float[] _crop;
    private float _scale_x=1,_scale_y=1;
    private int _scale_up=0,_scale_down=3;
    private int display_width,display_height;

    private Canvas _canvas;
    private Texture texture;
    private Paint _paint0,_paint1;

    BitmapText(int width,int height){
        display_width=width;
        display_height=height;
        _w=width/3;
        _h=height/5;
        //create a canvas, set color and rotate it
        _canvas=new Canvas();
//        _canvas.drawARGB(0,0,0,255);
        //create fonts . in this case typefaces
        Typeface menu=Typeface.createFromAsset(
                GameActivity.game.getAssets(),"nasaliza.ttf");
        Typeface timer=Typeface.createFromAsset(
                GameActivity.game.getAssets(),"stenc_ex.ttf");
        //create paint with that draw point and fps
        _paint0=new Paint();
        _paint0.setARGB(255,0,255,0);
        _paint0.setTextSize(_h/3);
        _paint0.setDither(true);
        _paint0.setTypeface(menu);
//        _paint0.setAntiAlias(true);
        _paint0.setStyle(Style.FILL_AND_STROKE);

        //create paint with that draw time
        _paint1=new Paint();
        _paint1.setARGB(255,255,0,255);
        _paint1.setTextSize(_h*2/3);
        _paint1.setDither(true);
        _paint1.setTypeface(timer);
//        _paint1.setAntiAlias(true);
        _paint1.setStyle(Style.FILL_AND_STROKE);

        _crop=new float[]{0,0,_w,_h};
    }

    public void drawMenu(GL10 gl,String fps,String points){
        _canvas.save();
        //create bitmap
        Bitmap _bitmap=Bitmap.createBitmap(_w,_h,Config.ARGB_8888);
        //add bitmap to canvas and draw text
        _canvas.setBitmap(_bitmap);
        _canvas.drawARGB(0,0,0,0);
        _canvas.drawText(points,10,_h*2/5,_paint0);
        if(OptionsLibrary.fps)
            _canvas.drawText(fps,10,_h*4/5,_paint0);
        
        if(texture==null)
            texture=TextureFactory.loadTexture(_bitmap,GL10.GL_TEXTURE_2D);
        else
            texture.setData(TextureFactory.makeBufferFromBitmap(
            		_bitmap,_bitmap.getWidth(),_bitmap.getHeight(),0,0));
        DrawTexture.draw(gl,texture);
        DrawTexture.drawAsImage(gl,texture,new Vector(0,display_height-_h,0),_crop);

        _bitmap.recycle();//destroy the bitmap
        _canvas.restore();
    }

    public void drawTime(GL10 gl,String time){
        _canvas.save();

        //create bitmap
        Bitmap _bitmap=Bitmap.createBitmap(_w,_h,Config.ARGB_8888);
        
        if(_scale_up!=3 && _scale_down==3){
            _scale_up++;
            _scale_x*=1.02f;
            _scale_y*=1.02f;
            if(_scale_up==3)
                _scale_down=0;
        }
        else if(_scale_down!=3 && _scale_up==3){
            _scale_down++;
            _scale_x*=1/1.02f;
            _scale_y*=1/1.02f;
            if(_scale_down==3)
                _scale_up=0;
        }

        _canvas.scale(_scale_x,_scale_y);

        //add bitmap to canvas and draw text
        String nulls="";
        for(int i=0;i<5-time.length();++i)
            nulls+="0";
        _canvas.setBitmap(_bitmap);
        _canvas.drawARGB(0,0,0,0);
        _canvas.drawText(time+nulls,10,_h*2/3,_paint1);        

        if(texture==null)
            texture=TextureFactory.loadTexture(_bitmap,GL10.GL_TEXTURE_2D);
        else
            texture.setData(TextureFactory.makeBufferFromBitmap(
            		_bitmap,_bitmap.getWidth(),_bitmap.getHeight(),0,0));
        DrawTexture.draw(gl,texture);
        DrawTexture.drawAsImage(gl,texture,
                new Vector((display_width-_w*_scale_x)/2,display_height-_h,0),_crop);

        _bitmap.recycle();//destroy the bitmap
        _canvas.restore();
    }
}